﻿using fuzhu;
using LH.Model.ST;
using SqlSuarEntity;
using SqlSugar;
using System.Data;
using XXX.Bo;
using XXX.Common;
using XXX.Models;
using XXX.Models.MO;

namespace Yonyou.U8.BO
{
    //取glmodel，为来源参照
    public class MOrderVM:API_Morder
    {
        //当前的数据库连接字符串
        public string conStr { get; set; }
        public string cacc { get; set; }
        public string xmlConfig { get; set; }
        public static string cDatabase = "";


        public MOrderVM()
        {
            //conStr
            conStr = CanShu.conStr;
            cacc = CanShu.cZT;
            //初始化定义xmlconfig
            xmlConfig = XmlOperation.GetConfigXML();
        }
        /// <summary>
        ///  根据HuaJinMorder 生成生产订单，非标生产订单
        /// </summary>
        /// <param name="dtMo">订单表头信息</param>
        /// <param name="dtOrderDetail">订单行</param>
        /// <param name="dtAllocate">订单子件</param>
        /// <returns></returns>
        public string InsertMo()
        {


            try
            {

                //取得账套号
                string cAcc = cacc;
                //id值减去获取条件，后续在新增
                int[] arrayMoid = DbHelper2.GetU8ID(cAcc, "mom_order", 1,  conStr);
                int iMoid = arrayMoid[0] - 1;

                //1个订单，1行数据 ，固定值1
                int cntOrderdetail = ItemList.Count;
                int[] arrayModid = DbHelper2.GetU8ID(cAcc, "mom_orderdetail", cntOrderdetail,conStr);
                int iModidf = arrayModid[0] - cntOrderdetail;
                int iModid = arrayModid[1] - cntOrderdetail; 

                iMoid += 1;
                
                mom_order mom_Order  = new mom_order();
                mom_Order.MoCode = cMoCode;
                mom_Order.CreateUser = cMaker; 
                mom_Order.MoId = iMoid; 
                mom_Order.UpdCount = 0;
                mom_Order.CreateDate = DbHelper.GetDbDate(DateTime.Now.ToString("yyyy-MM-dd"));
                mom_Order.CreateTime = DateTime.Now;
                //模版固定，没有使用取数功能
                mom_Order.VTid = 30413;
                mom_Order.RelsVTid = 30422;
                mom_Order.iPrintCount = 0;

                List<mom_orderdetail> lstorderdetails = new List<mom_orderdetail>();
                List<mom_morder> lstmorder = new List<mom_morder>();
                //子件，也放到一起
                List<mom_moallocate> lstmateril = new List<mom_moallocate>();
                for (int i=0;i< ItemList.Count;i++)
                {
                    iModidf += 1;
                    iModid += 1;
                    MorderDetail model = ItemList[i];
                    mom_orderdetail mom_Orderdetail = new mom_orderdetail();
                    mom_Orderdetail.MoDId = iModid;
                    mom_Orderdetail.MoId = iMoid;
                    
                    mom_Orderdetail.SortSeq = model.SortSeq;
                    ////非标生产订单
                    mom_Orderdetail.MoClass = model.MoClass;
                    mom_Orderdetail.Qty = DbHelper.GetDbdecimal(model.iQuantity);
                    mom_Orderdetail.MrpQty = DbHelper.GetDbdecimal(model.iQuantity);
                    mom_Orderdetail.SoType = DbHelper.GetDbInt(model.SoType);
                    mom_Orderdetail.SoDId = DbHelper.GetDbString(model.SoDid);
                    mom_Orderdetail.SoCode = DbHelper.GetDbString(model.cSoCode);
                    mom_Orderdetail.SoSeq = DbHelper.GetDbInt(model.SoSeq);
                    mom_Orderdetail.CustCode = model.CustCode;
                    mom_Orderdetail.Status = 3;
                    mom_Orderdetail.OrgStatus = 2;
                    mom_Orderdetail.BomId = model.BomId;
                    mom_Orderdetail.RoutingId = 0;
                    mom_Orderdetail.CustBomId = 0;
                    mom_Orderdetail.DemandId = 0;
                    mom_Orderdetail.PartId = DbHelper.GetDbInt(model.Partid);
                    mom_Orderdetail.InvCode = model.cInvCode;
                    mom_Orderdetail.AlloVTid = 30417;
                    mom_Orderdetail.RelsAlloVTid = 30426;
                    //选择bom = 主bom
                    mom_Orderdetail.BomType =1;
                    mom_Orderdetail.MDeptCode = model.cDepCode;
                    mom_Orderdetail.WhCode = model.WhCode;
                    mom_Orderdetail.MoTypeId = DbHelper.GetDbInt(model.MoTypeId);
                    mom_Orderdetail.OrderType = DbHelper.GetDbInt(model.SoType);
                    mom_Orderdetail.OrderDId = DbHelper.GetDbInt(model.SoDid);
                    mom_Orderdetail.OrderCode = DbHelper.GetDbString(model.cSoCode);
                    mom_Orderdetail.OrderSeq = DbHelper.GetDbInt(model.SoSeq);
                    //工序号码
                    mom_Orderdetail.Define22 = model.Define22;
                    mom_Orderdetail.Define23 = model.Define23;
                    mom_Orderdetail.SfcFlag = false;
                    mom_Orderdetail.CrpFlag = false;
                    mom_Orderdetail.QcFlag = false;
                    mom_Orderdetail.Free1 = "";
                    mom_Orderdetail.Free2 = "";
                    mom_Orderdetail.Free3 = "";
                    mom_Orderdetail.Free4 = "";
                    mom_Orderdetail.Free5 = "";
                    mom_Orderdetail.Free6 = "";
                    mom_Orderdetail.Free7 = "";
                    mom_Orderdetail.Free8 = "";
                    mom_Orderdetail.Free9 = "";
                    mom_Orderdetail.Free10 = "";
                    mom_Orderdetail.DeclaredQty = 0;
                    mom_Orderdetail.QualifiedInQty = 0;
                    mom_Orderdetail.RelsUser = cHandler;
                    mom_Orderdetail.RelsTime = DateTime.Now;
                    mom_Orderdetail.RelsDate = DbHelper.GetDbDate(model.dBeginDate);
                    mom_Orderdetail.AuditStatus = 1;
                    mom_Orderdetail.FmFlag = false;
                    mom_Orderdetail.SourceQCVouchType = 0;
                    mom_Orderdetail.PAllocateId = 0;
                    mom_Orderdetail.CollectiveFlag = 0;
                    mom_Orderdetail.iPrintCount = 0;
                    mom_Orderdetail.RunCardFlag = false;
                    mom_Orderdetail.RequisitionFlag = false;
                    mom_Orderdetail.RoutingType = 0;
                    mom_Orderdetail.IsWFControlled = 0;
                    mom_Orderdetail.iVerifyState = 0;
                    mom_Orderdetail.iReturnCount = 0;
                    mom_Orderdetail.SourceMoId = 0;
                    mom_Orderdetail.SourceMoDId = 0;
                    mom_Orderdetail.SourceQCId = 0;
                    mom_Orderdetail.SourceQCDId = 0;
                    mom_Orderdetail.ReformFlag = false;
                    mom_Orderdetail.OrgQty = 0;
                    mom_Orderdetail.LeadTime = 0;
                    mom_Orderdetail.OpScheduleType = 3;
                    mom_Orderdetail.OrdFlag = false;
                    mom_Orderdetail.WIPType = 5;
                    mom_Orderdetail.SourceSvcVouchType = 0;
                    //辅助换算
                    mom_Orderdetail.AuxUnitCode = model.AuxUnitCode;
                    mom_Orderdetail.AuxQty = model.AuxQty;
                    mom_Orderdetail.ChangeRate = model.ChangeRate;
                    //计划
                    mom_Orderdetail.PlanCode = model.PlanCode;
                    mom_Orderdetail.DemandId = DbHelper.GetDbInt(model.DemandId);
                    lstorderdetails.Add(mom_Orderdetail);

                    if (DbHelper.GetDbDate(model.dEndDate) < DbHelper.GetDbDate(model.dBeginDate))
                        model.dEndDate = model.dBeginDate; 

                    mom_morder mom_Morder = new mom_morder();
                    mom_Morder.MoDId = iModid;
                    mom_Morder.StartDate = model.dBeginDate;
                    mom_Morder.DueDate = model.dEndDate;
                    mom_Morder.MoId = iMoid;
                    lstmorder.Add(mom_Morder);
                   

                    //如果有扩展，这里添加
                    //mom_orderdetail_extradefine mom_Orderdetail_Extradefine = new mom_orderdetail_extradefine();
                    //mom_Orderdetail_Extradefine.MoDId = iModid;

                    //添加子件
                   if (model.MaterialList!=null)
                   {
                        //1个订单，1行数据 ，固定值1
                        int cntMaterial = model.MaterialList.Count;
                        int[] arrayAllocateid = DbHelper2.GetU8ID(cAcc, "mom_moallocate", cntMaterial, conStr);
                        int iAllocatefid = arrayAllocateid[0] - cntMaterial;
                        int iAllocateid = arrayAllocateid[1] - cntMaterial; ;

                        for ( int j = 0; j < model.MaterialList.Count; j++)
                        {
                            Material material = model.MaterialList[j];
                            iAllocateid++;
                            mom_moallocate mom_Moallocate = new mom_moallocate();
                            mom_Moallocate.MoDId = iModid;
                            mom_Moallocate.AllocateId = iAllocateid;
                            mom_Moallocate.SortSeq = material.iRowNo;
                            mom_Moallocate.OpSeq = material.opSeq;
                            mom_Moallocate.ComponentId = material.Componentid;
                            mom_Moallocate.BaseQtyN = material.dBaseQtyN;
                            mom_Moallocate.BaseQtyD = 1;
                            mom_Moallocate.ParentScrap = 0;
                            mom_Moallocate.CompScrap = 0; 
                            mom_Moallocate.BaseQtyD = 1;                            
                            mom_Moallocate.Qty = material.dQty;
                            mom_Moallocate.IssQty = 0;
                            mom_Moallocate.DeclaredQty = 0;
                            mom_Moallocate.WIPType = material.WIPType;
                            mom_Moallocate.StartDemDate = model.dBeginDate;
                            mom_Moallocate.EndDemDate = model.dEndDate;
                            mom_Moallocate.InvCode = material.cChildCode;
                            mom_Moallocate.OpComponentId = material.OpComponentId;
                            mom_Moallocate.AuxUnitCode = material.AuxUnitCode;
                            mom_Moallocate.ChangeRate = material.ChangeRate; 
                            mom_Moallocate.AuxBaseQtyN = material.AuxBaseQtyN;
                            mom_Moallocate.AuxQty = material.AuxQty;
                            mom_Moallocate.UpperMoQty =DbHelper.GetDbdecimal(model.iQuantity);
                            mom_Moallocate.FVFlag = 1;
                            mom_Moallocate.ByproductFlag = false;
                            mom_Moallocate.QcFlag = false;
                            mom_Moallocate.Offset = 0;
                            mom_Moallocate.ReplenishQty = 0;
                            mom_Moallocate.TransQty = 0;
                            mom_Moallocate.ProductType = 1;
                            mom_Moallocate.WhCode = material.WhCode;
                            mom_Moallocate.SoType = 0;
                            mom_Moallocate.SoDId = "";
                            mom_Moallocate.QmFlag = false;
                            mom_Moallocate.OrgQty = 0;
                            mom_Moallocate.OrgAuxQty = 0;
                            mom_Moallocate.RequisitionFlag = false;
                            mom_Moallocate.RequisitionQty = 0;
                            mom_Moallocate.RequisitionIssQty = 0;
                            mom_Moallocate.CostWIPRel = false;
                            mom_Moallocate.PickingQty  = 0;
                            mom_Moallocate.PickingAuxQty = 0;
                            mom_Moallocate.InvAlloeFlag = false;
                            mom_Moallocate.Free1 = "";
                            mom_Moallocate.Free2 = "";
                            mom_Moallocate.Free3 = "";
                            mom_Moallocate.Free4 = "";
                            mom_Moallocate.Free5 = "";
                            mom_Moallocate.Free6 = "";
                            mom_Moallocate.Free7 = "";
                            mom_Moallocate.Free8 = "";
                            mom_Moallocate.Free9 = "";
                            mom_Moallocate.Free10 = "";
                            //如果有替代料，这里添加
                            //替代料

                            lstmateril.Add(mom_Moallocate);
                 
                        }
                    }
                }
                 

                SqlSugarClient Tardb = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString =conStr,
                    DbType = SqlSugar.DbType.SqlServer,
                    IsAutoCloseConnection = true
                });
                try
                {

                    Tardb.BeginTran();
                    Tardb.Insertable(mom_Order).ExecuteCommand();
                    Tardb.Insertable(lstorderdetails).ExecuteCommand();
                    Tardb.Insertable(lstmorder).ExecuteCommand();
                    Tardb.Insertable(lstmateril).ExecuteCommand();
                    //更新已下计划
                    foreach (var detail in lstorderdetails)
                    {
                        string sql = "update mps_netdemand set crdqty= isnull(crdqty,0)+@iQty where DemandId = @DemandId";
                        Tardb.Ado.ExecuteCommand(sql, new { iQty = detail.Qty, DemandId = detail.DemandId });
                    }
                   
                   

                    Tardb.CommitTran();
                }
                catch (Exception ex)
                {
                    Tardb.RollbackTran();
                    throw ex;
                }
                finally
                {
                    // 显式关闭连接
                    Tardb.Ado.Connection?.Close();
                }
            }
            catch (Exception exception)
            {

                return ("导入失败：" + exception.Message);
            }


            return "ok";
        }



      /// <summary>
      /// 更新生产订单，需要重写，改成sqlsugar重写
      /// </summary>
      /// <returns></returns>
        public string UpdateMo()
        {

//            SqlTransaction tran = DbHelper2.BeginTrans(conStr);
//            try
//            {
//                //mom_order 不用更改，
//                //mom_orderdetail, 看数量是否有变化，然后更改
//                //mom_moallocate，看子件和数量是否有变化，然后更改

//                #region 生产订单,写入更改时间
//                string sql1 = @" update mom_order set ModifyDate=@ModifyDate,ModifyUser=@ModifyUser,ModifyTime=getdate()
//where moid = @id";
//                DbHelper2.ExecuteSqlWithTrans(sql1, new SqlParameter[]{
//                                         new SqlParameter("@id",Moid),
//                                         new SqlParameter("@ModifyDate",  DateTime.Now.ToString()),
//                                         new SqlParameter("@ModifyUser", cMaker)
//                    }, CommandType.Text, tran);

//                #endregion

//                #region 生产订单明细
//                //检查入库数量，是否一样
//                string sql = string.Format(@"select MoClass,qty as iQuantity,MoTypeId,OrderType,OrderDId,OrderCode,orderseq,partid,invcode,QualifiedInQty 
//from mom_orderdetail where modid={0}", MoDid);
//                List<mom_orderdetail> list = SqlsugarHelper.Instance.Ado.SqlQuery<mom_orderdetail>(sql);
//                if (list != null && list.Count > 0)
//                {
//                    HuaJinMorder huaJinMorder1 = list[0];
//                    //产品相同的情况下， 入库大于订单数量
//                    if (huaJinMorder1.cInvCode == model.cInvCode)
//                    {
//                        if (DbHelper.GetDbdecimal(huaJinMorder1.iQuantity) < DbHelper.GetDbdecimal(model.QualifiedInQty))
//                        {
//                            throw new Exception("订单入库大于工单数量，不可更改");
//                        }
//                    }
//                    else
//                    {
//                        //产品不同的情况下，数量有更改， 有入库，就不可以进行更改
//                        if (DbHelper.GetDbdecimal(huaJinMorder1.iQuantity) != DbHelper.GetDbdecimal(model.QualifiedInQty) && DbHelper.GetDbdecimal(model.QualifiedInQty) > 0)
//                        {
//                            throw new Exception("订单已入库，不可更改产品");
//                        }
//                    }

//                    //更新生产订单明细
//                    sql1 = string.Format(@"update mom_orderdetail set
// MoClass=@MoClass,qty=@qty,MrpQty =@MrpQty,MoTypeId=@MoTypeId, OrderDId=@OrderDId,OrderCode=@OrderCode,
// orderseq=@orderseq,partid=@partid,invcode=@invcode 
//from mom_orderdetail where modid=@modid");
//                    DbHelper2.ExecuteSqlWithTrans(sql1, new SqlParameter[]{
//                                         new SqlParameter("@modid",model.MoDid),
//                                         new SqlParameter("@qty",model.iQuantity),
//                                         new SqlParameter("@MrpQty",model.iQuantity),
//                                         new SqlParameter("@partid",model.Partid),
//                                         new SqlParameter("@invcode",model.cInvCode),
//                                           new SqlParameter("@MoTypeId",DbHelper.ToDbValue(model.MoTypeId)),
//                                             new SqlParameter("@OrderType",DbHelper.ToDbValue(model.OrderType)),
//                                               new SqlParameter("@OrderDId",DbHelper.ToDbValue(model.OrderDId)),
//                                                 new SqlParameter("@OrderCode",DbHelper.ToDbValue(model.OrderCode)),
//                                                  new SqlParameter("@orderseq",DbHelper.ToDbValue(model.OrderSeq)),
//                                                   new SqlParameter("@moclass",DbHelper.ToDbValue(model.MoClass))
//                    }, CommandType.Text, tran);
//                }
//                #endregion

//                # region  日期表，直接更新
//                if (DbHelper.GetDbDate(model.dEndDate) < DbHelper.GetDbDate(model.dBeginDate))
//                    model.dEndDate = model.dBeginDate;

//                sql1 = string.Format(@"update  dbo.mom_morder set  StartDate=@StartDate,DueDate=@DueDate where modid =@modid ");
//                DbHelper2.ExecuteSqlWithTrans(sql1, new SqlParameter[]{
//                                         new SqlParameter("@modid",model.MoDid),
//                                         new SqlParameter("@StartDate", model.dBeginDate),
//                                         new SqlParameter("@DueDate", model.dEndDate)
//                    }, CommandType.Text, tran);
//                #endregion


//                DbHelper2.CommitTransAndCloseConnection(tran);
//            }
//            catch (Exception exception)
//            {
//                DbHelper2.RollbackAndCloseConnection(tran);
//                return ("更新失败：" + exception.Message);
//            }

            return "err";
        }


        /// <summary>
        ///  删除生产订单
        ///  </summary> 
        public RetModel2 DelMo_WDFT(string cCode)
        {
            try
            {
                string sql = string.Format(@"select moid from mom_order where mocode='{0}'", cCode);
                string moid = DbHelper.GetDbString(DbHelper2.ExecuteScalar(sql, conStr));
                if (string.IsNullOrEmpty(moid))
                {
                    //代表没有生产订单
                    return DBService.ReErrObject("当前系统中没有此生产订单号");
                }
                sql = string.Format(@"select modid from mom_orderdetail where moid='{0}'", moid);
                string modid = DbHelper.GetDbString(DbHelper2.ExecuteScalar(sql, conStr));
                if (string.IsNullOrEmpty(modid))
                {
                    //代表没有生产订单
                    return DBService.ReErrObject("当前系统中没有此生产订单明细");

                }
                //删除产成品入库单
               
                

                ST_Rdrecord10.Rd10Head rdrecord10 = new ST_Rdrecord10.Rd10Head(); 
                List<ST_Rdrecord10.Rd10Body> lstrk = new List<ST_Rdrecord10.Rd10Body>();
                RdRecord10VM rdRecord10VM = new RdRecord10VM(rdrecord10, lstrk);
                string rkid = "";
                //检查出入库关联
                sql = string.Format(@" select  a.id from rdrecord10 a
inner join  rdrecords10 b on a.id = b.id 
inner join mom_orderdetail d on b.impoids = d.modid
where d.modid = {0} ", modid);
                DataTable dtRK=DbHelper2.ExecuteTable(sql, CanShu.conStr);
                if (dtRK.Rows.Count > 0)
                {
                    for (int i = 0; i < dtRK.Rows.Count; i++)
                    {
                        rdRecord10VM.head.id = DbHelper.GetDbString(dtRK.Rows[i]["id"]);
                        RetModel2 retck = rdRecord10VM.UnverifyDelete();
                        if (retck.code != 200)
                        {
                            throw new Exception("产成品入库单：" + retck.message);
                        }
                    }
                }
               
                
                   
                

                //检查出入库关联
                sql = string.Format(@" select distinct  a.id from rdrecord11 a
inner join  rdrecords11 b on a.id = b.id
inner join  mom_moallocate c on b.impoids = c.allocateid
inner join mom_orderdetail d on c.modid = d.modid
where d.modid ={0} ", modid);
                ST_Rdrecord11.Rd11Head rdrecord11 = new ST_Rdrecord11.Rd11Head(); 
                List<ST_Rdrecord11.Rd11Body> lstck = new List<ST_Rdrecord11.Rd11Body>();
                RdRecord11VM rdRecord11VM = new RdRecord11VM(rdrecord11, lstck);
                DataTable dtCK = DbHelper2.ExecuteTable(sql, CanShu.conStr);
                if (dtCK.Rows.Count> 0)
                {
                    for (int i = 0; i < dtCK.Rows.Count; i++)
                    {
                        rdRecord11VM.head.id = DbHelper.GetDbString(dtCK.Rows[i]["id"]);
                        RetModel2 retck = rdRecord11VM.UnverifyDelete();
                        if (retck.code != 200)
                        {
                            throw new Exception("材料出库单：" + retck.message);
                        }
                    }
                }
                
                 
                SqlSugarClient Tardb = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = conStr,
                    DbType = SqlSugar.DbType.SqlServer,
                    IsAutoCloseConnection = true
                });
                try
                {


                    Tardb.BeginTran();
                    if (!string.IsNullOrEmpty(modid))
                    {
                        Tardb.Deleteable<mom_moallocate>()
                           .Where(s => s.MoDId == DbHelper.GetDbInt(modid))
                          .ExecuteCommand();
                        Tardb.Deleteable<mom_morder>()
                                    .Where(s => s.MoDId == DbHelper.GetDbInt(modid))
                                   .ExecuteCommand();
                        //Tardb.Deleteable<mom_orderdetail_extradefine>()
                        //           .Where(s => s.MoDId == DbHelper.GetDbInt(modid))
                        //          .ExecuteCommand();
                        Tardb.Deleteable<mom_orderdetail>()
                                     .Where(s => s.MoDId == DbHelper.GetDbInt(modid))
                                        .ExecuteCommand();
                    }
                    Tardb.Deleteable<mom_order>()
                                 .Where(s => s.MoId == DbHelper.GetDbInt(moid))
                                .ExecuteCommand();


                    Tardb.CommitTran();

                }
                catch (Exception exception)
                {
                    Tardb.RollbackTran();
                  throw new Exception("生产订单删除失败：" + exception.Message);
                }

               return DBService.ReOKObject("删除完成");
            }
            catch(Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }
          
        }

        //获取生产订单对应的存货编码，生产部门
        public mom_orderdetail GetMoItem(string cMoCode, string copcode)
        {

            using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = conStr,
                DbType = SqlSugar.DbType.SqlServer,
                IsAutoCloseConnection = true
            }))
            {
                mom_orderdetail mom =db.Queryable<mom_orderdetail>()
                    .InnerJoin<mom_order>((a,b)=>a.MoId==b.MoId&&b.MoCode==cMoCode &&a.Define22==copcode)
                    .First();
                return mom;
            }
        }


        //获取生产订单材料信息，领料用
        public mom_moallocate GetMoMaterialItem(string cMoCode, string cinvcode)
        {

            using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = conStr,
                DbType = SqlSugar.DbType.SqlServer,
                IsAutoCloseConnection = true
            }))
            {
                mom_moallocate mom = db.Queryable<mom_moallocate>()
                    .InnerJoin<mom_orderdetail>((a,b)=>a.MoDId ==b.MoDId)
                    .InnerJoin<mom_order>((a, b,c) => c.MoId == b.MoId )
                    .Where((a, b,c) =>  c.MoCode== cMoCode && a.InvCode == cinvcode)
                    .First();
                return mom;
            }
        }

        //获取生产订单材料信息，领料用
        public mom_orderdetail GetMoDetailItem(int modid)
        {

            using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = conStr,
                DbType = SqlSugar.DbType.SqlServer,
                IsAutoCloseConnection = true
            }))
            {
                mom_orderdetail mom = db.Queryable<mom_orderdetail>() 
                    .Where(a => a.MoDId ==modid)
                    .First();
                return mom;
            }
        }

        /// <summary>
        /// 检查生产订单号是否存在
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Exists(ref string id)
        {
            if (string.IsNullOrEmpty( cMoCode))
            {
                return false;
            }

            using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = conStr,
                DbType = SqlSugar.DbType.SqlServer,
                IsAutoCloseConnection = true
            }))
            {
                mom_order _Order = db.Queryable<mom_order>().Where(x => x.MoCode == cMoCode).First();
                if (_Order != null)
                {
                    id = _Order.MoId.ToString();
                    return true;
                }
                else 
                {
                    return false;
                }
              

            }

        }
    }
}
